<?php


namespace Hlcreation\Basic;

use PhpOffice\PhpWord\IOFactory;
use Mpdf\Mpdf;
use TCPDF;

class FileTransfers
{

    /**
     * @param $path 需要转换的文件地址
     * @param string $del  是否删除转换出的html文件
     * @param string $html_path  存放html的地址
     * @return false|string|string[]
     * @throws \PhpOffice\PhpWord\Exception\Exception
     */
    public static function wordToHtml($path, $del = 1, $html_path = "/upload/contract/tmp"){
        $file = $_SERVER['DOCUMENT_ROOT']."/".$path;
        $phpWord = IOFactory::load($file);
        $xmlWriter = IOFactory::createWriter($phpWord, "HTML");
        // html 存放目录
        $html_dir = $_SERVER['DOCUMENT_ROOT'].$html_path;
        if (!is_dir($html_dir)) { mkdir($html_dir,0777,true);   }
        // html存放的文件名
        $file_name = Helper::getMillisecond().".html";

        $html_file = $html_dir."/".$file_name;
        // 将word保存为html
        $xmlWriter->save($html_file);
        // 读取html
        $info = file_get_contents($html_file);

        // 修改文件的某些样式
        $info = str_replace('PHPWord', '', $info);
        // 将不识别的下划线转换为____
        $info = str_replace('underline ;">','underline ;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $info);

        if ($del === 1) {
            @unlink($html_file);
            return $info;
        } else {
            return ['html'=>$info, 'path'=>$html_path."/".$file_name];
        }
    }


    // html转换为pdf
    public static function htmlToPdf($html, $pdf_path = "upload/contract/pdf/"){
        // html 存放目录
        $html = str_replace(["<p></p>", "<p>&nbsp;</p>"],"", $html);
        $file_name = Helper::getMillisecond().'.pdf';
        $full_dir = $_SERVER['DOCUMENT_ROOT']."/".$pdf_path;
        Helper::checkDir($full_dir);
        $file = $full_dir.$file_name;
        $config = [
            'mode' => 'zh-CN',
            'format' => 'A4',
            'default_font' => '宋体'
        ];
        $mpdf = new Mpdf($config);
        $mpdf->WriteHTML($html);
        $mpdf->Output($file,'F');//保存pdf文件到指定目录
        return $pdf_path."/".$file_name;
    }



    // html转换为pdf
    public static function wordToPdf($part, $html, $pdf_path = "/upload/contract/pdf"){
        // html 存放目录
        $html = str_replace("<p></p>","", $html);
        $file_name = Helper::getMillisecond().'.pdf';
        $full_dir = $_SERVER['DOCUMENT_ROOT'].$pdf_path;
        Helper::checkDir($full_dir);
        $file = $full_dir."/".$file_name;

//        $pdf = new TCPDF('P', 'mm', 'A4', true, 'zh-CN');
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        $pdf->SetFont('stsongstdlight', '', 10);
        $pdf->SetMargins(20, 5,20);
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        $pdf->AddPage();
        $pdf->writeHTML($html);
        /**
         * tcpdf x 0最左侧， x为210 最右侧（209可看到红边）
         *       y 0为最顶， y为246为最下，超过246会跑到下一页最顶部
         */
        foreach ($part as $p) {

            if (!empty($p['yinzhang'])){
                $yinzhang_page = ceil($p['positionY'] / 246/2);
                $yinzhang_y = ceil($p['positionY'] % 246);
                $pdf->setPage($yinzhang_page);
                $pdf->Image($p['yinzhang'], $p['positionX'], $yinzhang_y, 30, 8, 'png', '', '', false,300);
//                $pdf->Image($p['yinzhang'], 209, 0, 30, 8, 'png', '', '', false,300);

            }
//            if (!empty($p['gongzhang'])){
//                $gongzhang_page = ceil($p['positionfirmY'] / 246);
//                $gongzhang_y = ceil($p['positionY'] % 246);
//                $pdf->setPage($gongzhang_page);
//
////                $pdf->Image($p['gongzhang'], 0, 246, 30, 30, 'png', '', '', false,300);
//                $pdf->Image($p['gongzhang'], $p['positionfirmX'], $gongzhang_y, 30, 30, 'png', '', '', false,300);
//
//            }

        }
//        $pdf->setPage(1);
//        $pdf->Image('http://192.168.1.200/sign/1/1610528617128.png', 201, 100, 30, 30, 'png', '', '', false,300);
//        $pdf->setPage(2);
//        $pdf->Image('http://192.168.1.200/sign/1/1610528617128.png', 101, 100, 30, 30, 'png', '', '', false,300);
        $pdf->Output($file,'F');//保存pdf文件到指定目录
        return $pdf_path."/".$file_name;
    }

    public static function imgToPdf($img,  $pdf_path = "upload/contract/pdf/"){
        $html = "<img style='width:100%;height:100%' src='".$img." '/>";
        $file_name = Helper::getMillisecond().'.pdf';
        $full_dir = $_SERVER['DOCUMENT_ROOT']."/".$pdf_path;
        Helper::checkDir($full_dir);
        $file = $full_dir.$file_name;
        $config = [
            'mode' => 'zh-CN',
            'format' => 'A4',
            'default_font' => '宋体'
        ];
        $mpdf = new Mpdf($config);
        $mpdf->WriteHTML($html);
        $mpdf->Output($file,'F');//保存pdf文件到指定目录
        return $pdf_path."/".$file_name;
    }

}
